/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.renren.modules.project.entity.DatasourceEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Mapper
public interface DatasourceDao extends BaseMapper<DatasourceEntity> {
    @Select("select ds.*,scs.name as serverName,scs.ip_addr as serverIpaddr from datasource ds left join sys_config_server scs  on ds.server_id = scs.id where ds.name like #{key}")
    DatasourceEntity queryByKey(String key);
    @Select("select ds.*,scs.name as serverName,scs.ip_addr as serverIpaddr from datasource ds left join sys_config_server scs  on ds.server_id = scs.id  limit ${size},${limit} ")
    List<DatasourceEntity> selectEntityPage(String key, long size, long limit);
    @Select("select count(1) from datasource ")
    long selectRecordSize(String key);
    @Select("select ds.*,scs.name as serverName,scs.ip_addr as serverIpaddr from datasource ds left join sys_config_server scs  on ds.server_id = scs.id " +
            "where ds.id in ( select database_id from module m where m.backend_id in (select id from backend b where b.project_id = #{id}))" +
            " or ds.id in ( select mongodb_id from module m where m.backend_id in (select id from backend b where b.project_id = #{id}))" +
            " or ds.id in ( select redis_id from module m where m.backend_id in (select id from backend b where b.project_id = #{id}))")
    List<DatasourceEntity> selectListByProjectId(Long id);
    @Select("select ds.*,scs.name as serverName,scs.ip_addr as serverIpaddr from datasource ds left join sys_config_server scs  on ds.server_id = scs.id")
    List<DatasourceEntity> findeAll();
}
